package com.example.activity.data;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.example.activity.data.databinding.ActivitySecondBinding;

import java.util.ArrayList;

public class SecondActivity extends AppCompatActivity {
    private ActivitySecondBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySecondBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Intent intent = getIntent();
        String data = intent.getStringExtra("data");
        if (data != null) {
            binding.tvData.setText("获取的字符串为：" + data);
        }

        ArrayList<Integer> datas = intent.getIntegerArrayListExtra("list");
        if (datas != null) {
            binding.tvData.setText("获取的列表数据为：" + datas.toString());
        }

        User user = (User) intent.getSerializableExtra("object");
        if (user != null) {
            binding.tvData.setText("获取的对象数据为：" + user.toString());
        }

        Bundle bundle = intent.getExtras();
        if(bundle != null) {
            String name = bundle.getString("username");
            int age = bundle.getInt("age");
            if(name != null) {
                binding.tvData.setText("获取的bundle数据为：" + name + ", " + age);
            }
        }

        binding.btnBack.setOnClickListener(v -> {
            Intent result = new Intent();
            result.putExtra("data", "返回FirstActivity");
            setResult(RESULT_OK, result);
            finish();
        });
    }
}